package com.mhsoft.fenak.map;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.mhsoft.fenak.utils.MyGeoClass;
import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;

public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
	private Activity activity;
	
	private ArrayList<MyGeoClass> pathPoints;
	private int color;
	
	public MyItemizedOverlay(Drawable defaultMarker, MapView mapView, Activity a, ArrayList<MyGeoClass> pathPoints, int color) {
		super(boundCenter(defaultMarker), mapView);
		
		this.activity = a;
		
		this.pathPoints = pathPoints;
		this.color = color;
	}

	public void addOverlay(OverlayItem overlay) {
	    m_overlays.add(overlay);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return m_overlays.get(i);
	}

	@Override
	public int size() {
		return m_overlays.size();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		
		Paint paint = new Paint();
		
        Point from = new Point();
        Point to = new Point();
		
        for(int i = 0; i < pathPoints.size(); i++){
        	
        	// to avoid the out of index exception at (i+1) ...
        	if(i == pathPoints.size()-1){
        		break;
        	}
        	
        	// converts the given GeoPoint to onscreen pixel coordinates, relative to the top-left of the MapView that provided this Projection ...
        	mapView.getProjection().toPixels(new MyGeoClass(pathPoints.get(i).getLatitudeE6(), pathPoints.get(i).getLongitudeE6()), from);

            mapView.getProjection().toPixels(new MyGeoClass(pathPoints.get(i+1).getLatitudeE6(), pathPoints.get(i+1).getLongitudeE6()), to);

            paint.setStrokeWidth(2);
            paint.setAntiAlias(true);
//            paint.setARGB(50, 3, 180, 200);
            paint.setColor(color);
            paint.setStyle(Style.STROKE);
            
            // dashed-line ...
//            paint.setPathEffect(new DashPathEffect(new float[] {15,5,5,5}, 0));
            canvas.drawLine(from.x, from.y, to.x, to.y, paint);
        }
		
		// pass false to remove the shadow ...
		super.draw(canvas, mapView, false);
	}

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		
		
		
		return true;
	}
	
}